package com.tongren.framework.sample;

import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;
import com.tongren.framework.R;
import com.tongren.framework.callback.JsonCallback;
import com.tongren.framework.core.BaseActivity;
import com.tongren.framework.core.CustomApplication;
import com.tongren.framework.model.BasePageModel;
import com.tongren.framework.model.BaseQA;
import com.tongren.framework.model.BaseResult;
import com.tongren.framework.model.BaseUser;
import com.tongren.framework.widgets.pull.BaseViewHolder;
import com.tongren.framework.widgets.pull.PullRecycler;

import java.util.ArrayList;

import static com.tongren.framework.constants.ConstantValues.BASE_IMG_URL;
import static com.tongren.framework.constants.ConstantValues.BASE_URL;

/**
 * zhangxu
 */
public class QAInfoActivity extends BaseActivity {

    private int page = 1;
    private boolean isInitCache = false;
    private TextView tv_qa_title;
    private TextView tv_qa_user;
    private TextView tv_qa_date;
    private TextView tv_qa_realurl;
    private Button btn_qa;
    private String id = "";
    private String name = "";
    private EditText et_text;

    @Override
    protected void setUpTitle(int titleResId) {
        super.setUpTitle(R.string.title_qa_info);
    }

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_qa_info,-1);
    }

    @Override
    protected void setUpView() {
        tv_qa_title = (TextView) findViewById(R.id.tv_qa_title);
        tv_qa_user = (TextView) findViewById(R.id.tv_qa_user);
        tv_qa_date = (TextView) findViewById(R.id.tv_qa_date);
        btn_qa = (Button) findViewById(R.id.btn_qa);
        et_text = (EditText) findViewById(R.id.et_text);
    }

    @Override
    protected void setUpData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String createName = intent.getStringExtra("createName");
        String date = intent.getStringExtra("date");
        id = intent.getStringExtra("id");
        name = intent.getStringExtra("name");
        tv_qa_title.setText(Html.fromHtml(title));
        tv_qa_user.setText("作者:"+createName);
        tv_qa_date.setText("日期:"+date);

        btn_qa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseUser user = CustomApplication.getInstance().getUserModel();
                OkGo.<BaseResult<BasePageModel<ArrayList<BaseQA>>>>post(BASE_URL+"saveConsultationQuestion.ajax")//请求方式和请求url
                        .tag(this)//请求的 tag, 主要用于取消对应的请求
                        .params("yhxlh", user.getXlh())//
                        .params("content", et_text.getText().toString())//
                        .params("consultationId", id)//
                        .params("yhmc", name)//
                        .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)  //缓存模式先使用缓存,然后使用网络数据
                        .execute(new JsonCallback<BaseResult<BasePageModel<ArrayList<BaseQA>>>>() {
                            @Override
                            public void onSuccess(Response<BaseResult<BasePageModel<ArrayList<BaseQA>>>> response) {
//                        handleResponse(response);
                                String str = response.toString();
                                System.out.println(str);

                                String code = response.body().code;
                                if ("0".equals(code)){
                                    Toast.makeText(QAInfoActivity.this, "回复成功", Toast.LENGTH_SHORT).show();
                                }

                            }

                            @Override
                            public void onCacheSuccess(Response<BaseResult<BasePageModel<ArrayList<BaseQA>>>> response) {
                                //一般来说,只需呀第一次初始化界面的时候需要使用缓存刷新界面,以后不需要,所以用一个变量标识
                                if (!isInitCache) {
                                    //一般来说,缓存回调成功和网络回调成功做的事情是一样的,所以这里直接回调onSuccess
                                    onSuccess(response);
                                    isInitCache = true;
                                }
                            }

                            @Override
                            public void onError(Response<BaseResult<BasePageModel<ArrayList<BaseQA>>>> response) {
//                        handleError(response);
                            }
                        });
            }
        });

    }







    class SampleViewHolder extends BaseViewHolder {

        ImageView iv_new_img;
        TextView tv_title;
        TextView tv_date;
//        TextView tv_state;

        public SampleViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_new_title);
//            tv_date = (TextView) itemView.findViewById(R.id.tv_new_date);
//            tv_state = (TextView) itemView.findViewById(R.id.tv_state);
//            iv_new_img = (ImageView) itemView.findViewById(R.id.iv_new_img);
        }

        @Override
        public void onBindViewHolder(int position) {
//            Glide.with(iv_new_img.getContext())
//                    .load(mDataList.get(position).getPhoto())
//                    .centerCrop()
//                    .placeholder(R.color.app_primary_color)
//                    .crossFade()
//                    .into(iv_new_img);
//            tv_title.setText(mDataList.get(position).getDocumentName());
//            String date = DateHelper.timeStamp2Date(mDataList.get(position).getCreateDate(),"yyyy-MM-dd HH:mm");
//            tv_date.setText(date);
//            tv_state.setText(mDataList.get(position).getState());

        }

        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(QAInfoActivity.this, "dianji"+position, Toast.LENGTH_LONG).show();
        }

    }
}
